package com.hnichr.ump.assessment.service.impl;

import com.beisen.sdk.assessment.model.ActivityInfo;
import com.beisen.sdk.assessment.model.ActivityInfoList;
import com.hnichr.ump.common.assessment.entity.Activity;
import com.hnichr.ump.common.assessment.service.ActivityService;
import com.hnichr.ump.common.assessment.service.AssessmentInterfService;
import com.hnichr.ump.common.assessment.service.AssessmentService;
import com.hnichr.ump.common.assessment.vo.ActivitySelectVO;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl extends BaseServiceImpl<Activity> implements ActivityService {

    @Autowired
    private AssessmentInterfService interfService;

    @Autowired
    private AssessmentService assessmentService;

    @Transactional
    @Override
    public List<Activity> getByBeisenActivityId(String bsActivityId){
        return JpaUtil.linq(Activity.class).equal("bsActivityId",bsActivityId).list();
    }

    @Transactional
    @Override
    public List<ActivitySelectVO> select(){
        List<ActivityInfo> activityList = assessmentService.getAllActivityFromBeisen();
        List<ActivitySelectVO> list = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(activityList)){
            for(ActivityInfo activityInfo:activityList){
                if(!StringUtils.equals(String.valueOf(activityInfo.state),"1")){
                    continue;
                }
                ActivitySelectVO selectVO = new ActivitySelectVO();
                selectVO.setActivityId(String.valueOf(activityInfo.id));
                selectVO.setActivityName(activityInfo.name);
                selectVO.setState(String.valueOf(activityInfo.state));
                selectVO.setActivityDesc(activityInfo.description);
                try{
                    selectVO.setCreateDate(DateUtils.parseDate(activityInfo.create_date,"yyyy-MM-dd'T'HH:mm:ss.SSS"));
                }
                catch (Exception e){

                }
                list.add(selectVO);
            }
        }
        return list;
    }

    @Transactional
    @Override
    public String addActivity(Activity activity){
        List<Activity> activityList = JpaUtil.linq(Activity.class).equal("bsActivityId",activity.getBsActivityId()).equal("tenantId",activity.getTenantId()).list();
        if(CollectionUtils.isNotEmpty(activityList)){
            return "同一个活动一个租户仅能添加一次";
        }
        super.save(activity);
        return null;
    }

    @Transactional
    @Override
    public String editActivity(Activity activity){
        List<Activity> activityList = JpaUtil.linq(Activity.class).equal("bsActivityId",activity.getBsActivityId()).equal("tenantId",activity.getTenantId()).list();
        if(CollectionUtils.isEmpty(activityList)){
            super.update(activity);
            return null;
        }
        for(Activity a:activityList){
            if(!StringUtils.equals(a.getId(),activity.getId())){
                return "同一个活动一个租户仅能添加一次";
            }
        }
        super.update(activity);
        return null;
    }
}
